// 中间件
const express = require('express')
const server = express()


// 提供一个req.time的中间件
const time = (isFormat) => {
  return (req, res, next) => {
    let now = new Date()
    if (isFormat) {
      req.time = `${now.getFullYear()}-${now.getMonth()+1}-${now.getDate()} ${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}`
      next()
    } else {
      req.time = now.getTime()
      next()
    }
  }
}

//传入参数true做格式化,不传就不做格式化
server.use(time(true))

server.get('/', (req, res) => {
  console.log(req.time)
  res.send('hello')
})

server.listen(8080)